package com.hogan.hoganproject.home.adapter;

import android.text.Layout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.hogan.hoganproject.R;
import com.hogan.hoganproject.base.view.EmptyViewHolder;
import com.hogan.hoganproject.base.view.XRecyclerView;
import com.hogan.hoganproject.home.api.JuHeBean;

import java.util.List;

/**
 * Author: chenhu
 * Time: 2020/9/25 10:39
 * Description do somethings
 */
public class HomeAdapter extends XRecyclerView.XAdapter<RecyclerView.ViewHolder> {
    private List<JuHeBean.ResultBean.DataBean> mDataBeanList;

    public HomeAdapter() {
    }

    public HomeAdapter(List<JuHeBean.ResultBean.DataBean> dataBeanList) {
        mDataBeanList = dataBeanList;
    }

    @Override
    protected int getxItemCount() {
        return mDataBeanList != null ? mDataBeanList.size() : 0;
    }

    @Override
    protected RecyclerView.ViewHolder onCreateXViewHolder(ViewGroup viewGroup, int itemType) {
        View itemView;
        if (itemType == XRecyclerView.EMPTY_VIEW) {
            itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.empty_view_layout, viewGroup, false);
            return new EmptyViewHolder(itemView);
        } else {
            itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_fragment_item_layout, viewGroup, false);
            return new HomeViewHolder(itemView);
        }

    }

    @Override
    protected void onBindXViewHolder(RecyclerView.ViewHolder holder, int position) {
        JuHeBean.ResultBean.DataBean bean = mDataBeanList.get(position);
        if (!TextUtils.isEmpty(bean.getTitle())) {
            if (holder instanceof HomeViewHolder) {
                ((HomeViewHolder) holder).mTextView.setText(bean.getTitle());
            }
        }
    }

    @Override
    protected XRecyclerView.LoadMoreViewHolder createLoadMoreViewHolder(ViewGroup viewGroup) {
        return new XRecyclerView.LoadMoreViewHolder() {
            @Override
            public View createLoadMoreView() {

                return LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.refresh_load_progressbar_view, viewGroup, false);
            }

            @Override
            public void changeLoadMoreViewState(boolean loading) {
                View bar = mLoadMoreView.findViewById(R.id.load_more_progressBar);
                bar.setVisibility(loading ? View.VISIBLE : View.GONE);
            }
        };

    }
}
